Skip to content

Conversation

@ThierryBerger
Copy link
Member

@ThierryBerger ThierryBerger commented Sep 7, 2022

Fix #5315

Also:

  • use a triangle mesh + vertex shader for using clip space
  • use a post_processing module to ease integration within existing projects
  • unrelated fix a TextureFormat

@ThierryBerger
Copy link
Member Author

Coming from annieversary#1, where the resizing is working 🎉

Unfortunately, at the current state of this PR, the resizing doesn't work as expected, but I'm unsure why.

I'm gonna work through differences betweens these similar looking codes and try to fix it. If anyone has ideas you're most welcome to share 👍

@ThierryBerger ThierryBerger force-pushed the 5315-fix-post-processing-resizing branch from 9c8a571 to a885e03 Compare September 7, 2022 07:38
Copy link
Member Author

@ThierryBerger ThierryBerger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resizing is working 🎉

post_processing_resizing.mp4
addressed issues

I need to research a bit how to limit the updates to the post processing material

This was adressed by bd87a21#diff-d7696ffdd45f974d95c03442261f195c34b196b587b29db22ad1ecd6eae7f4acR149

  • also, there is a column artifact on the side of the screen which you can see in following gif.

This is actually a shader specific problem, to have chromatic aberration working on the sides, we should render to a larger rendertarget image and/or have a bigger fullscreen triangle. I don't want to address this issue in this PR, it was already existing previously.
Another solution is to change the shader example to something simpler like a "black and white" or blur effect 🤷.

@ThierryBerger ThierryBerger added A-Rendering Drawing game state to the screen C-Examples An addition or correction to our examples labels Sep 7, 2022
.spawn_bundle(Camera2dBundle {
camera: Camera {
// renders after the first main camera which has default value: 0.
priority: camera.priority + 10,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed that from priority: 1 because it makes it easier to make it work with split screen example

Comment on lines +227 to +250
let post_processing_pass_layer =
RenderLayers::layer((RenderLayers::TOTAL_LAYERS - 1) as u8);
let half_extents = Vec2::new(size.width as f32 / 2f32, size.height as f32 / 2f32);
let mut triangle_mesh = Mesh::new(PrimitiveTopology::TriangleList);
// NOTE: positions are actually not used because the vertex shader maps UV and clip space.
triangle_mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
[-half_extents.x, -half_extents.y, 0.0],
[half_extents.x * 3f32, -half_extents.y, 0.0],
[-half_extents.x, half_extents.y * 3f32, 0.0],
],
);
triangle_mesh.set_indices(Some(Indices::U32(vec![0, 1, 2])));
triangle_mesh.insert_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0]],
);

triangle_mesh.insert_attribute(
Mesh::ATTRIBUTE_UV_0,
vec![[2.0, 0.0], [0.0, 2.0], [0.0, 0.0]],
);
let triangle_handle = meshes.add(triangle_mesh);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've written that positions are not used, but it's probably a lie, as if I change them a bit I notice it's not working properly ; I'm open to suggestions to explain what's happening here.

Copy link
Contributor

@rparrett rparrett Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least on my end (macos/m1), things seem to work properly as long as some positions are set, even

triangle_mesh.insert_attribute(
    Mesh::ATTRIBUTE_POSITION,
    vec![[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]],
);

commands
.entity(entity)
// add the handle to the camera so we can access it and change the percentages
.insert(material_handle.clone())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not so fan of adding a raw handle as component but at least it's simple.

Comment on lines +253 to +258
let material_handle = post_processing_materials.add(PostProcessingMaterial {
source_image: image_handle.clone(),
offset_r: Vec2::new(0.1f32, 0.1f32),
offset_g: Vec2::new(0.1f32, -0.1f32),
offset_b: Vec2::new(-0.1f32, -0.1f32),
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those will get overwritten quickly in the update_material

@ThierryBerger ThierryBerger marked this pull request as ready for review September 7, 2022 13:24
Copy link
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of naming nitpicks.

Can you explain why you couldn't use a Quad like the original example? This is example is already pretty complicated and using a custom mesh complicates it even more.

Co-authored-by: Charles <IceSentry@users.noreply.github.com>
@ThierryBerger
Copy link
Member Author

Can you explain why you couldn't use a Quad like the original example?

It was a feedback from the original post processing PR: #4797 (review)

I was mostly curious to see if I could implement it correctly and have feedbacks about it.

You surely have a good point though, I can make it into a separate PR, where we could focus on benchmarking it too.

@IceSentry
Copy link
Contributor

Right, I guess a fullscreen triangle would be better. Personally, it feels a bit out of scope for the example, but it does make sense to push people in that direction until we have first party support.

@ThierryBerger
Copy link
Member Author

ThierryBerger commented Sep 8, 2022

Right, I guess a fullscreen triangle would be better. Personally, it feels a bit out of scope for the example, but it does make sense to push people in that direction until we have first party support.

It might be wiser to exclude it indeed from that PR, as when looking into #5902 ; I think it's a better way of implementing a fullscreen triangle than what I did.

But removing the shader vertex makes the resizing to fail (the quad no longer takes the full screen) ; so I'll let it as-is and wait for more feedbacks.

ChristopherBiscardi added a commit to rust-adventure/bevy-examples that referenced this pull request Sep 12, 2022
option_window_id = Some(*window_id);
Extent3d {
width: window.physical_width(),
height: window.physical_height(),
Copy link
Contributor

@darthdeus darthdeus Nov 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect and doesn't work with UI scaling. The correct solution is either

let scale = window.scale_factor();

// ...

width: (window.physical_width() as f64 / scale) as u32,
height: (window.physical_height() as f64 / scale) as u32,

or just the simpler alternative that already returns the logical dimensions

width: window.width() as u32,
height: window.height() as u32,

let window = windows.get(fit_to_window.window_id).expect("PostProcessingCamera is rendering to a window, but this window could not be found");
Extent3d {
width: window.physical_width(),
height: window.physical_height(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rparrett
Copy link
Contributor

It would be nice if a rendering SME could weigh in, but it seems like the vertex shader may be the cleanest way to support resizing properly. If that's the case, I feel like it is worth the extra complexity.

I think we could at least tuck the mesh generation code into a separate function.

@rparrett
Copy link
Contributor

rparrett commented Feb 15, 2023

Actually, this seems to exist now: https://docs.rs/bevy/latest/bevy/core_pipeline/fullscreen_vertex_shader/index.html

Is that stuff useful here?

@ThierryBerger
Copy link
Member Author

I'm closing this because the example was completely reworked with #8376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Examples An addition or correction to our examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Post processing example doesn't support resizing

4 participants